const Store = require("../models/store");

class StoreController {
  // 获取所有门店
  static async getAllStores(req, res) {
    console.log("Received request to get all stores");
    try {
      const stores = await Store.getAll();
      res.json({
        code: 200,
        success: true,
        data: stores,
      });
    } catch (error) {
      console.error("Error getting stores:", error);
      res.status(500).json({ success: false, message: "获取门店列表失败" });
    }
  }

  // 获取附近门店
  static async getNearbyStores(req, res) {
    try {
      const { latitude, longitude } = req.query;
      if (!latitude || !longitude) {
        return res
          .status(400)
          .json({ success: false, message: "请提供当前位置的经纬度" });
      }
      const stores = await Store.getNearbyStores(
        parseFloat(latitude),
        parseFloat(longitude)
      );
      res.json({
        code: 200,
        success: true,
        data: stores,
      });
    } catch (error) {
      console.error("Error getting nearby stores:", error);
      res.status(500).json({ success: false, message: "获取附近门店失败" });
    }
  }

  // 获取单个门店
  static async getStoreById(req, res) {
    try {
      const { id } = req.params;
      const store = await Store.getById(id);
      if (!store) {
        return res.status(404).json({ success: false, message: "门店不存在" });
      }
      res.json({
        code: 200,
        success: true,
        data: store,
      });
    } catch (error) {
      console.error("Error getting store:", error);
      res.status(500).json({ success: false, message: "获取门店详情失败" });
    }
  }

  // 创建门店
  static async createStore(req, res) {
    try {
      const storeData = req.body;
      const storeId = await Store.create(storeData);
      console.log(111111, storeId);
      res.status(201).json({
        code: 200,
        success: true,
        data: { id: storeId },
      });
    } catch (error) {
      console.error("Error creating store:", error);
      res.status(500).json({ success: false, message: "创建门店失败" });
    }
  }

  // 更新门店
  static async updateStore(req, res) {
    try {
      const { id } = req.params;
      const storeData = req.body;
      const success = await Store.update(id, storeData);
      if (!success) {
        return res.status(404).json({ success: false, message: "门店不存在" });
      }
      res.json({
        code: 200,
        success: true,
        message: "更新成功",
      });
    } catch (error) {
      console.error("Error updating store:", error);
      res.status(500).json({ success: false, message: "更新门店失败" });
    }
  }

  // 删除门店
  static async deleteStore(req, res) {
    try {
      const { id } = req.params;
      const success = await Store.delete(id);
      if (!success) {
        return res.status(404).json({ success: false, message: "门店不存在" });
      }
      res.json({
        code: 200,
        success: true,
        message: "删除成功",
      });
    } catch (error) {
      console.error("Error deleting store:", error);
      res.status(500).json({ success: false, message: "删除门店失败" });
    }
  }

  // 分页获取门店
  static async getStoresByPage(req, res) {
    console.log("Received request to get stores by page");
    try {
      const { page = 1, pageSize = 10, keyword } = req.query;
      const offset = (page - 1) * pageSize;
      
      const [stores, total] = await Promise.all([
        keyword ? 
          Store.searchByName(keyword, offset, pageSize) : 
          Store.getByPage(offset, pageSize),
        keyword ? 
          Store.getSearchCount(keyword) : 
          Store.getTotalCount()
      ]);

      res.json({
        code: 200,
        success: true,
        data: {
          items: stores,
          total,
          page: Number(page),
          pageSize: Number(pageSize)
        }
      });
    } catch (error) {
      console.error("Error getting stores by page:", error);
      res.status(500).json({ success: false, message: "获取门店分页数据失败" });
    }
  }
}

module.exports = StoreController;
